<?
include("../header.php");
include("../lib/config.php");
$op		= $HTTP_GET_VARS['op'];
$ks		= $HTTP_GET_VARS['kodeso'];
$level	= $HTTP_GET_VARS['level'];

if($op == "edit"){
	$Faction = "../lib/update.php";
	$Vbutton = "Update";
	$query	= mssql_query("SELECT * FROM MstSO WHERE kode_so='$ks'",$conn);
	$row	= mssql_fetch_array($query);
}
else{
	$Faction = "../lib/save.php";
	$Vbutton = "Simpan";
}

?>
<script type="text/javascript" language="javascript">
	function Validate(){
		if(document.F.kode_so1.value == "")
		{
			alert("Field : KodeSO tidak boleh kosong ...");
			document.F.kode_so1.focus();
			return false;
		}
		
		if(document.F.uraian.value == "")
		{
			alert("Field : Uraian / Nama Struktur Organisasi tidak boleh kosong ...");
			document.F.uraian.focus();
			return false;
		}
		
		if(document.F.levelso.value == "")
		{
			alert("Field : Level SO harus dipilih ...");
			document.F.levelso.focus();
			return false;
		}

		if(document.F.golongan.value == "")
		{
			alert("Field : Golongan harus dipilih ...");
			document.F.golongan.focus();
			return false;
		}
		
		if(document.F.status.value == "")
		{
			alert("Field : Status harus dipilih ...");
			document.F.status.focus();
			return false;
		}

		if(document.F.kode_lokasi.value == "")
		{
			alert("Field : Kode Lokasi harus dipilih ...");
			document.F.kode_lokasi.focus();
			return false;
		}

		if(document.F.area_code.value == "")
		{
			alert("Field : Area Code harus dipilih ...");
			document.F.area_code.focus();
			return false;
		}

		document.F.submit();

	}
	
	function show_popup2(page)
	{
		child = open
		(
			"get_divisi_p.php",
			"",
			"scrollbars,width=600,height=400,left=150,top=150"
		);
	}
	
</script>
</head>
<form name="F" method="post" action="<?=$Faction?>">
<table width="100%" class="no_border">
 <tr>
  <td>&nbsp;</td>
 </tr>
 <tr>
  <td>
 <table cellpadding="2" cellspacing="0" width="100%">
 <?
 	if($level == 1 && empty($op)){
 	// get level 1 direktorat
	$sqldir	= mssql_query("SELECT * FROM VLevelDirektorat ORDER BY kode_so DESC",$conn);
	$rowdir = mssql_fetch_array($sqldir);
	//	echo"$rowdir[kode_so]";
	$level1	= substr($rowdir[kode_so],0,2); // direktorat
	$level2 = substr($rowdir[kode_so],2,2); // koordinator
	$level3 = substr($rowdir[kode_so],4,3); // cabang biro 
	$level4 = substr($rowdir[kode_so],7,3); // bagian
	$level5 = substr($rowdir[kode_so],10,2); //seksi
	
		if(($level1+1) < 10){
			$kode_so = "0".($level1+1)."0000000000";
		}
		else{
			$kode_so = ($level1+1);		
		}
	}
	
 	elseif($level == 2 && empty($op)){
 	// get level 1 direktorat
	$sqldir	= mssql_query("SELECT * FROM VLevelKoordinator ORDER BY kode_so DESC",$conn);
	$rowdir = mssql_fetch_array($sqldir);
	//	echo"$rowdir[kode_so]";
	$level1	= substr($rowdir[kode_so],0,2); // direktorat
	$level2 = substr($rowdir[kode_so],2,2); // koordinator
	$level3 = substr($rowdir[kode_so],4,3); // cabang biro 
	$level4 = substr($rowdir[kode_so],7,3); // bagian
	$level5 = substr($rowdir[kode_so],10,2); //seksi
	
		if(($level1+1) < 10){
			$kode_so = "0".($level1+1)."0000000000";
		}
		else{
			$kode_so = ($level1+1)."0000000000";		
		}
	}
	
 ?>
 <tr>
  <td width="150">KODE SO</td>
  <td>
  <input type="text" name="kode_so1" value="<?
 	if($level == 1 && empty($op)){
 		echo"$kode_so";
	}
	elseif($level == 2 || $level == 3 || $level == 4 || $level == 5 && empty($op)){
		echo"auto generated";
	}
	else{
		echo"$row[kode_so]";
	}
  ?>" readonly="readonly" maxlength="100" size="50" /> <span class="required">*</span></td>
 </tr>
 <?
	if($level == 2 && empty($op)){
 ?>
 <tr>
  <td>Direktorat</td>
  <td><input type="text" size="50" readonly="readonly" name="direktorat1" />
  <input type="hidden" readonly="readonly" name="koor_teknis1" />
  <input type="hidden" readonly="readonly" name="divisi1" />
  <input type="hidden" readonly="readonly" name="bagian1" />
  <input type="hidden" readonly="readonly" name="seksi1" />
  <input type="hidden" readonly="readonly" name="lokasi1" />
  <input type="hidden" readonly="readonly" name="kode_so" /><a href="Javascript:show_popup2()"><img src="../images/folder_txt.png" border="0" /></a></td>
 </tr>
 <?
	}
	if($level == 3 && empty($op)){
 ?>
 <tr>
  <td>Direktorat</td>
  <td><input type="text" size="50" readonly="readonly" name="direktorat1" /> <a href="Javascript:show_popup2()"><img src="../images/folder_txt.png" border="0" /></a></td>
 </tr>
 <tr>
  <td>Koor. Teknis</td>
  <td><input type="text" size="50" readonly="readonly" name="koor_teknis1" /></td>
 </tr>
  <input type="hidden" readonly="readonly" name="divisi1" />
  <input type="hidden" readonly="readonly" name="bagian1" />
  <input type="hidden" readonly="readonly" name="seksi1" />
  <input type="hidden" readonly="readonly" name="lokasi1" />
  <input type="hidden" readonly="readonly" name="kode_so" />
   </td>
 </tr>
 <?
	}
	if($level == 4 && empty($op)){
 ?>
 <tr>
  <td>Direktorat</td>
  <td><input type="text" size="50" readonly="readonly" name="direktorat1" /> <a href="Javascript:show_popup2()"><img src="../images/folder_txt.png" border="0" /></a></td>
 </tr>
 <tr>
  <td>Koor. Teknis</td>
  <td><input type="text" size="50" readonly="readonly" name="koor_teknis1" /></td>
 </tr>
  <td>Cabang/Divisi/Biro/</td>
  <td><input type="text" readonly="readonly" name="divisi1" />
  <input type="hidden" readonly="readonly" name="bagian1" />
  <input type="hidden" readonly="readonly" name="seksi1" />
  <input type="hidden" readonly="readonly" name="lokasi1" />
  <input type="hidden" readonly="readonly" name="kode_so" />
   </td>
 </tr>
 <?
	}
	//}
	if($level == 5 && empty($op)){
 ?>
 <tr>
  <td>Direktorat</td>
  <td><input type="text" size="50" readonly="readonly" name="direktorat1" /> <a href="Javascript:show_popup2()"><img src="../images/folder_txt.png" border="0" /></a></td>
 </tr>
 <tr>
  <td>Koor. Teknis</td>
  <td><input type="text" size="50" readonly="readonly" name="koor_teknis1" /></td>
 </tr>
  <td>Cabang/Divisi/Biro/</td>
  <td><input type="text" readonly="readonly" name="divisi1" /></td>
 </tr>
 <tr>
  <td>Bagian</td>
  <td><input type="text" readonly="readonly" name="bagian1" />
  <input type="hidden" readonly="readonly" name="seksi1" />
  <input type="hidden" readonly="readonly" name="lokasi1" />
  <input type="hidden" readonly="readonly" name="kode_so" />
   </td>
 </tr>
 <?
	}
 ?>
 <tr>
  <td>Uraian / Nama Posisi <? if($level == 2){ echo"Koor. Teknis"; }?></td>
  <td><input type="text" name="uraian" maxlength="100" size="50" value="<?=$row[uraian]?>" /> <span class="required">*</span></td>
 </tr>
 <tr>
  <td>Level SO</td>
  <td>
  <input type="text" name="levelso" readonly="readonly" value="<?
   	if($level == 1 && empty($op)){
 		echo"$level";
	}
	elseif($level == 2 || $level == 3 || $level == 4 || $level == 5 && empty($op)){
		echo"$level";
	}
	else{
		echo"$row[LevelSO]";
	}

  ?>" />
  </td>
 </tr>
 <tr>
  <td>Golongan</td>
  <td>
   <select name="golongan" style="width:75px;">
   	<?
	if($op == "edit"){
		echo"<option value='$row[golongan]'>$row[golongan]</option>";
	}
	?>
    <option value="">--pilih--</option>
	<?
		for($j=1;$j<=17;$j++){
			echo"<option value='$j'>$j</option>";		
		}
	?>
   </select> <span class="required">*</span>
  </td>
 </tr>
 <tr>
  <td>Status</td>
  <td>
   <select name="status" style="width:75px;">
   <?
   if($op == "edit"){
   		if($row[status] == 1){
			echo"<option value='1'>Aktif</option>";
		}
		else{
			echo"<option value='0'>Tidak Aktif</option>";
		}
	}
   ?>
    <option value="">--pilih--</option>
	<option value="1">Aktif</option>
	<option value="0">Tidak Aktif</option>
   </select> <span class="required">*</span>
  </td>
 </tr>
 <tr>
  <td>Kode Lokasi</td>
  <td>
   <select name="kode_lokasi">
   <?
   if($op == "edit"){
		$sKLq = mssql_query("SELECT * FROM MstLokasi WHERE kode_lokasi='$row[kode_lokasi]'",$conn);
		$rKLq = mssql_fetch_array($sKLq);
		echo"
		 <option value='$rKLq[kode_lokasi]'>$rKLq[distrik]</option>
		";
   }
   ?>
    <option value="">--pilih--</option>
	<?
		$sKL = mssql_query("SELECT * FROM MstLokasi ORDER BY distrik ASC",$conn);
		while($rKL = mssql_fetch_array($sKL)){
			echo"<option value='$rKL[kode_lokasi]'>$rKL[distrik], $rKL[propinsi]</option>";
		}
	?>
   </select> <span class="required">*</span>
  </td>
 </tr>
 <tr>
  <td>Area Code</td>
  <td>
   <select name="area_code" style="width: 320px;">
   <?
   	if($op == "edit"){
		$sAC1 = mssql_query("SELECT * FROM VCabang WHERE Kode_Cabang='$row[AreaCode]'",$conn);
		$rAC1 = mssql_fetch_array($sAC1);
		echo"
		 <option value='$rAC1[Kode_Cabang]'>$rAC1[Kode_Cabang] - $rAC1[Nama]</option>
		";
	}
   ?>
    <option value="">--pilih--</option>
	<?
		$sAC = mssql_query("SELECT * FROM VCabang ORDER BY Nama",$conn);
		while($rAC = mssql_fetch_array($sAC)){
			echo"
			 <option value='$rAC[Kode_Cabang]'>$rAC[Kode_Cabang] - $rAC[Nama]</option>
			";
		}
	?>
   </select>
  </td>
 </tr>
 <tr>
  <td>&nbsp;</td>
  <td align="right">
  <br /><br />
  <input type="hidden" name="cat" value="tbl_unit_kerja" />
  <input type="button" value="Batal" onclick="Javascript:history.back();" class="button" />
  <input type="reset" value="Reset" class="button"/><input type="button" value="<?=$Vbutton?>" class="button" onClick="Validate();" />
  <br /><br />
  </td>
 </tr>
</table>
</form>
</td>
</tr>
<tr>
 <td colspan="2" style="border:none;"><? include("../footer.php"); ?></td>
</tr>
</table>
<?
echo"</div>
	  </td>
	   </tr>
	    </table>
";
include("../menu.php");
?>